package steed.hadmin.tag;

import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.jsp.JspException;

import steed.tag.BaseTag;

/**
 * 添加BootstrapTable到jsp
 * @author 战马
 *
 */
public class Table extends BaseTag{
	private static final long serialVersionUID = 1L;
	private boolean showCheckBox = true;
	private boolean showColumnNumber = true;
	public void setShowCheckBox(boolean showCheckBox) {
		this.showCheckBox = showCheckBox;
	}
	private Map<String, String> tableHeader;
	private String[] excludeFields;
	private boolean excludeAll = true;

	public boolean isExcludeAll() {
		return excludeAll;
	}

	public void setShowColumnNumber(boolean showColumnNumber) {
		this.showColumnNumber = showColumnNumber;
	}

	public void setExcludeAll(boolean excludeAll) {
		this.excludeAll = excludeAll;
	}


	public void setExcludeFields(String excludeFields) {
		this.excludeFields = excludeFields.replace(".", "__").split(",");
	}


	public void setTableHeader(Map<String, String> tableHeader) {
		this.tableHeader = tableHeader;
	}


	@Override
	public int doStartTag() throws JspException {
		writeFile("tableBegin");
		if (showCheckBox) {
			writeFile("checkBox");
		}
		if (showColumnNumber) {
			writeFile("columnNumber");
		}
		getTableHeader();
		if (!excludeAll && tableHeader != null) {
			removeExcludeFields();
			for(Entry<String, String> temp:tableHeader.entrySet()){
				write(String.format(getTagHtml("th"), temp.getKey(),temp.getValue()));
			}
		}
		return EVAL_BODY_INCLUDE;
	}

	
	
	@Override
	public int doEndTag() throws JspException {
		writeFile("tableEnd");
		return EVAL_PAGE;
	}


	private void getTableHeader() {
		if (tableHeader == null) {
			tableHeader = (Map<String, String>) pageContext.getRequest().getAttribute("tableHeader");
		}
	}


	private void removeExcludeFields() {
		if (excludeFields != null) {
			for (String temp:excludeFields) {
				tableHeader.remove(temp);
			}
		}
	}

}
